import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:qianyi/entity/message/Message.dart';
import 'package:video_player/video_player.dart';

import '../network/http_config.dart';
import '../network/http_entity.dart';
import '../network/http_manager.dart';
import 'package:http/http.dart' as http;

class ImageAvatarController extends GetxController {
  RxList<MemoryImage> avatarImages = RxList<MemoryImage>();
  final Map<String, MemoryImage> _memoryCache = {};
  RxBool isLoad = true.obs;

  RxList<VideoPlayerController> imageVideoDetailEntity = RxList();
  int current = 0;

  void setCurrent() {
    if(current == 3) {
      current = current % 3;
    } else {
      current = current + 1;
    }
  }
  VideoPlayerController getCurrent() {
    return this.imageVideoDetailEntity[current-1];
  }
  Future<void> addVideoPlayerController(VideoPlayerController videoPlayerController) async {
    if(imageVideoDetailEntity.length == 3) {
      this.imageVideoDetailEntity.add(videoPlayerController);

    } else {
      this.imageVideoDetailEntity[current].dispose();
      this.imageVideoDetailEntity[current] = videoPlayerController;
    }
    await this.imageVideoDetailEntity[current].initialize();
    this.imageVideoDetailEntity[current].setLooping(true);
  }




  Future<MemoryImage> saveImageToMemory(String imageUrl) async {
    if (_memoryCache.containsKey(imageUrl)) {
      return _memoryCache[imageUrl]!;
    }

    final response = await http.get(Uri.parse(imageUrl));
    if (response.statusCode == 200) {
      final memoryImage = MemoryImage(response.bodyBytes);
      _memoryCache[imageUrl] = memoryImage;
      return memoryImage;
    } else {
      throw Exception('Failed to load image');
    }
  }

  Future<void> setAvatar(Message item) async {
    avatarImages.clear();
    Map<String, String> params = {
      "uid": item.acceptId!,
    };
    Map<String, String> params2 = {
      "uid": item.sendId!,
    };

    HttpEntity httpEntity = await HttpManager.getInstance().request(url: "/platform/user/getUserInfo", method: Method.get, params: params) as HttpEntity;
    HttpEntity httpEntity1 = await HttpManager.getInstance().request(url: "/platform/user/getUserInfo", method: Method.get, params: params2) as HttpEntity;

    String acceptAvatarUrl = httpEntity.getOriData()['data']['avatar'];
    String sendAvatarUrl = httpEntity1.getOriData()['data']['avatar'];

    // Get the actual file URL for the avatar images
    Map<String, String> params3 = {
      "path": acceptAvatarUrl
    };
    HttpEntity httpEntity3 = await HttpManager.getInstance().request(url: '/util/fileoss/getFileUrl', params: params3, method: Method.get) as HttpEntity;
    Map<String, String> params4 = {
      "path": sendAvatarUrl
    };
    HttpEntity httpEntity4 = await HttpManager.getInstance().request(url: '/util/fileoss/getFileUrl', params: params4, method: Method.get) as HttpEntity;

    // Save images to memory and add to the list
    avatarImages.add(await saveImageToMemory(httpEntity3.getOriData()['data']));
    avatarImages.add(await saveImageToMemory(httpEntity4.getOriData()['data']));
    this.isLoad.value = false;
  }
}